package com.example.wisdomclassroomapi.common.util;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

/**
 * Rest接口访问工具类<br/>
 * 封装RestTemplate
 */
@RequiredArgsConstructor
@Slf4j
public class RestUtil {
    private final RestTemplate restTemplate;
    private final JsonUtil jsonUtil;

    public <T> T getForObject(String url, Class<T> clazz) {
        log.info("getForObject => start, url: {}, clazz: {}", url, clazz.getName());
        String responseMessage = restTemplate.getForObject(url, String.class);
        log.info("getForObject => responseMessage: {}", responseMessage);
        T result = jsonUtil.read(responseMessage, clazz);
        log.info("getForObject => end, result: {}", result);
        return result;
    }

}
